package com.mag.servlet;

import com.mag.domain.Suppliers;
import com.mag.service.SuppliersService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class SearchSupplier extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html");
        SuppliersService Ss = new SuppliersService();
        String type = request.getParameter("type");

        if ("ByID".equals(type)) {
            ArrayList<Suppliers> al = new ArrayList<>();
            String id = request.getParameter("SupplierID");
            Suppliers suppliers = Ss.GetSupplierBySupplierID(id);
            if (suppliers.getSupplierID() != null) {
                al.add(suppliers);
            }
            request.getSession().setAttribute("SupplierList", al);
            request.getSession().setAttribute("ison", "ByID");
            response.sendRedirect("/manager/jsp/SupplierManage.jsp");
        }
        if ("ByName".equals(type)) {
            ArrayList<Suppliers> al;
            String suppliername = request.getParameter("SupplierName");
            al = Ss.GetSupplierBySupplierName(suppliername);
            request.getSession().setAttribute("SupplierList", al);
            request.getSession().setAttribute("ison", "ByName");
            response.sendRedirect("/manager/jsp/SupplierManage.jsp");
        }
        if ("allsupplier".equals(type)) {
            ArrayList<Suppliers> al;
            al = Ss.GetAllSuppliers();
            request.getSession().setAttribute("SupplierList", al);
            request.getSession().setAttribute("ison", "allsupplier");
            response.sendRedirect("/manager/jsp/SupplierManage.jsp");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        this.doGet(request, response);
    }

}